package com.aisi.backend.mapper;

import com.aisi.backend.domain.entity.Topic;
import com.aisi.backend.domain.vo.response.TopicPreviewVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author: shenjianZ
 * @Date: 2024/5/11 下午9:15
 * @Description:
 */

@Mapper
public interface TopicMapper {


    // 查询所有主题
    @Select("SELECT * FROM topic")
    List<Topic> findAllTopics();

    // 根据ID查询主题
    @Select("SELECT * FROM topic WHERE id = #{id}")
    Topic findTopicById(@Param("id") Integer id);

    // 添加主题
    @Insert("INSERT INTO topic (title, content, type, time, uid) VALUES (#{title}, #{content}, #{type}, #{time}, #{uid})")
    Integer addTopic(Topic topic);

    // 更新主题
    Integer updateTopic(@Param("topic") Topic topic ,@Param("userId") Integer userId);

    // 删除主题
    @Delete("DELETE FROM topic WHERE id = #{id}")
    Integer deleteTopic(@Param("id") Integer id);

    @Select("""
        SELECT * ,account.username,account.avatar FROM topic LEFT JOIN account ON topic.uid = account.id
        order by `time` desc
        limit #{page},10
    """)
    List<Topic> listTopicAll(Integer page);

    @Select("""
        SELECT * FROM topic LEFT JOIN account ON topic.uid = account.id
        where type = #{type}
        order by `time` desc
        limit #{page},10
    """)
    List<Topic> listTopic(Integer page, Integer type);
}

